package com.how.config;

import com.google.common.collect.Lists;
import com.how.config.base.DataSourceBase;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

@Configuration
public class Console0DataSourceConfig extends DataSourceBase {

    @Lazy
    @Resource
    ShardingDataSource shardingDataSource;

    @Bean(CONSOLEDATABASE)
    public DataSource getDatasource() throws Exception {
        Map<String, DataSource> dataSourceMap = shardingDataSource.getDataSourceMap();

        //具体分库分表策略
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        // 不分库分表情况下的数据源制定--默认数据库
        shardingRuleConfig.setDefaultDataSourceName(CONSOLEDATABASE_0 + "-master");
        // 默认的分表规则
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
        // 默认的分库规则
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
        // 指定表的主键生成策略为SNOWFLAKE
        shardingRuleConfig.setDefaultKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));

        Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>();
        //
        MasterSlaveRuleConfiguration masterSlaveRuleConfiguration0
                = new MasterSlaveRuleConfiguration(CONSOLEDATABASE_0, CONSOLEDATABASE_0 + "-master",
                // 从库集合
                Lists.newArrayList(CONSOLEDATABASE_0 + "-slave1"),
                // 轮训策略
                new LoadBalanceStrategyConfiguration("round_robin"));

        masterSlaveRuleConfigs.add(masterSlaveRuleConfiguration0);

        //
        MasterSlaveRuleConfiguration masterSlaveRuleConfiguration1
                = new MasterSlaveRuleConfiguration(CONSOLEDATABASE_1, CONSOLEDATABASE_1 + "-master",
                // 从库集合
                Lists.newArrayList(CONSOLEDATABASE_1 + "-slave1"),
                // 轮训策略
                new LoadBalanceStrategyConfiguration("round_robin"));

        masterSlaveRuleConfigs.add(masterSlaveRuleConfiguration1);

        shardingRuleConfig.setMasterSlaveRuleConfigs(masterSlaveRuleConfigs);

        // 设置分表
        shardingRuleConfig.setTableRuleConfigs(ConsoleTableRuleConfig.getTableRuleConfigurations());

        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, getProperties());
    }
}